//require GoogleMaps
package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.googlemaps.MapContainer;
import com.codename1.googlemaps.MapContainer.MapObject;
import com.codename1.googlemaps.MapLayout;
import com.codename1.maps.BoundingBox;
import com.codename1.maps.Coord;
import com.codename1.maps.MapListener;
import com.codename1.maps.Mercator;

import com.codename1.ui.BrowserComponent;
import com.codename1.ui.Button;
import com.codename1.ui.Container;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.FontImage;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.ui.plaf.Style;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class GoogleMapsAddMarkerSample {
    private static final String JS_API_KEY = "AIzaSyDpB3cxxxxxxxxxxx53rYMbBnYe_kmTu0";
    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Coord location = new Coord(37.7831, -122.401558);
        Form mapDemo = new Form("Maps", new BorderLayout());
        
        if (BrowserComponent.isNativeBrowserSupported()) {
            System.out.println("Browser Component Supported");
            MapContainer mc = new MapContainer(JS_API_KEY);
            
            // Add buttons at top to provide an offset so we can test event 
            // coordinates properly.
            mapDemo.add(BorderLayout.NORTH, BoxLayout.encloseY(new Button("Button 1"), new Button("Button 2")));
            mapDemo.add(BorderLayout.CENTER, BorderLayout.centerEastWest(mc, null, new Button("Button 5")));


            Coord moscone = new Coord(37.7831, -122.401558);
            mc.zoom(moscone, 5);
            mc.setCameraPosition(moscone);
            
            mc.addTapListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    //Point of the screen where the user pressed
                    int x = evt.getX();
                    int y = evt.getY();
                    //I get the coordinates from the point of the screen
                    Coord coord = mc.getCoordAtPosition(x, y);
                    //I create the marker, with a style
                    Style s = new Style();
                    s.setFgColor(0xff0000);
                    s.setBgTransparency(0);
                    FontImage markerImg = FontImage.createMaterial(FontImage.MATERIAL_PLACE, s);
                    EncodedImage icon = EncodedImage.createFromImage(markerImg, false);
                    mc.addMarker(icon, coord, "My position", null, null);
                    mc.zoom(coord, 15);
                }
            });
            
            //mc.setShowMyLocation(true);
        } else {
            // iOS Screenshot process...
            mapDemo.add(new Label("Loading, please wait...."));
        }
        mapDemo.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
